#!/bin/bash

list="00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22"

n=0
for i in $list
do
	load[$n]=`qconf -se comp$i | grep load_avg | grep -v np_load | cut -c32-35`
	posn=`expr index "${load[$n]}" .`
	let posn="$posn-1"
	load2[$n]=`echo "${load[$n]}" | cut -c0-$posn`
	((n++))
	posn=-1
done

echo "------------------------------------------------------------------------"
echo "Displays the load factors for all the nodes of the cluster. A load factor"
echo "of 8 means there are 8 CPU intensive jobs running on that node and hence"
echo "that node is full. A load factor of 4 would indicate a node with 4 cores"
echo "in use and 4 free."
echo "------------------------------------------------------------------------"

WARN="no"
FREE="no"
FULL="no"

for (( j=0; j<=22; j++ ))
do
	num=${load2[$j]}
	if (( num >= 9 ))
	then
		WARN="yes"
		STATE[$j]=2
	elif (( num == 8 ))
	then
		FULL="yes"
		STATE[$j]=1
	elif (( num == 7 ))
	then
		FULL="yes"
		STATE[$j]=1
	else (( num <= 6 ))
		FREE="yes"
		STATE[$j]=0
	fi
done

if [ "$WARN" = "yes" ]
then
	echo ""
	echo "WARNING, nodes with abnormally high loads."
	echo "---"
	n=0
	for i in $list
	do
		if (( ${STATE[$n]} == 2 ))
		then
			echo "comp$i	${load[$n]}"
		fi
	((n++))
	done
fi

if [ "$FREE" = "yes" ]
then
	echo ""
        echo "Nodes with spare capacity."
        echo "---"
        n=0
        for i in $list
        do
                if (( ${STATE[$n]} == 0 ))
                then
                        echo "comp$i	${load[$n]}"
                fi
        ((n++))
        done
fi

if [ "$FULL" = "yes" ]
then
	echo ""
        echo "Full nodes."
        echo "---"
        n=0
        for i in $list
        do
                if (( ${STATE[$n]} == 1 ))
                then
                        echo "comp$i	${load[$n]}"
                fi
        ((n++))
        done
fi

